package com.ou.csbs.capstone.views.menu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.ou.csbs.capstone.models.AttributeModel;

import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Material;
import gov.nasa.worldwind.render.ShapeAttributes;
/**
 * The Shape Dialog Swing menu. This is shown whenever the Settings option in the toolbar is selected if you are not drawing or have not selected a Text object.
 * 
 * @author Senior Capstone
 */
public class ShapeDrawDialog extends JFrame{
	private static final long serialVersionUID = -5452830791096462092L;
	
	private Color newOutline;
	private ImageIcon[] stippleOptions = {new ImageIcon(this.getClass().getResource("stipple_1.gif")),
										  new ImageIcon(this.getClass().getResource("stipple_2.gif")),
										  new ImageIcon(this.getClass().getResource("stipple_3.gif")),
										  new ImageIcon(this.getClass().getResource("stipple_4.gif")),
										  new ImageIcon(this.getClass().getResource("stipple_5.gif"))};
	private String[] widthOptions = {"1", "2", "3", "4", "5"};

	private JPanel centerPanel = new JPanel();
	private JComboBox<ImageIcon> linePattern = new JComboBox<ImageIcon>(stippleOptions);
	private JComboBox<String> lineWidth = new JComboBox<String>(widthOptions);
	private JSlider outlineOpacity = new JSlider(SwingConstants.HORIZONTAL);
	private JButton outlineColor = new JButton();
	private JPanel southPanel = new JPanel();
	private JButton ok = new JButton("OK");
	private JButton cancel = new JButton("Cancel");
	private BasicShapeAttributes bsa;
	
	public ShapeDrawDialog(ShapeAttributes shapeAttributes) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());
		if(shapeAttributes != null){
			bsa = (BasicShapeAttributes) shapeAttributes;
		}
		else{
			bsa = AttributeModel.getAttributes();
		}
		
		newOutline = bsa.getOutlineMaterial().getDiffuse();
		
		linePattern.setSelectedIndex(getSelectedStippleIndex());
		lineWidth.setSelectedIndex((int) (bsa.getOutlineWidth()/2) - 1);
		outlineOpacity.setValue((int)(bsa.getOutlineOpacity()*100));
		outlineColor.setBackground(newOutline);
		ok.setPreferredSize(new Dimension(90,30));
		cancel.setPreferredSize(new Dimension(90,30));
		
		centerPanel.setLayout(new GridLayout(4, 2, 20, 20));
		centerPanel.add(new JLabel("Line Pattern:"));
		centerPanel.add(linePattern);
		centerPanel.add(new JLabel("Line Width:"));
		centerPanel.add(lineWidth);
		centerPanel.add(new JLabel("Outline Opacity"));
		centerPanel.add(outlineOpacity);
		centerPanel.add(new JLabel("Outline Color:"));
		centerPanel.add(outlineColor);
		
		southPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
		southPanel.add(ok);
		southPanel.add(cancel);
		
		add(centerPanel, BorderLayout.CENTER);
		add(southPanel, BorderLayout.SOUTH);
		pack();
		
		addActionListener();
		setVisible(true);
	}
	
	private void addActionListener() {
		outlineColor.addActionListener(new dialogListener());
		ok.addActionListener(new dialogListener());
		cancel.addActionListener(new dialogListener());
	}
	
	private short getSelectedStipplePattern(){
		switch(linePattern.getSelectedIndex()){
		case 1: return AttributeModel.STIPPLE_PATTERN_1;
		case 2: return AttributeModel.STIPPLE_PATTERN_2;
		case 3: return AttributeModel.STIPPLE_PATTERN_3;
		case 4: return AttributeModel.STIPPLE_PATTERN_4;
		default: return AttributeModel.STIPPLE_PATTERN_0;
		}
	}
	private int getSelectedStippleIndex(){
		switch(bsa.getOutlineStipplePattern()){
		case AttributeModel.STIPPLE_PATTERN_1: return 1;
		case AttributeModel.STIPPLE_PATTERN_2: return 2;
		case AttributeModel.STIPPLE_PATTERN_3: return 3;
		case AttributeModel.STIPPLE_PATTERN_4: return 4;
		default: return 0;

		}
	}
	
	private class dialogListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == ok) {
				bsa.setOutlineStipplePattern(getSelectedStipplePattern());
				bsa.setOutlineWidth((lineWidth.getSelectedIndex()+1)*2);
				bsa.setOutlineOpacity((double) outlineOpacity.getValue()/100.0);
				bsa.setOutlineMaterial(new Material(newOutline));
				AttributeModel.setAttributes(bsa);
				dispose();
			}
			else if(e.getSource() == cancel) {
				dispose();
			}
			else if(e.getSource() == outlineColor) {
				Color newColor = JColorChooser.showDialog(centerPanel, "Choose outline color", newOutline);
				if(newColor != null) {
					newOutline = newColor;
					outlineColor.setBackground(newOutline);
				}
			}
		}
	}
}
